package com.fi.common.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fi.common.domain.entity.ResAccessoryFileInfo;
import com.fi.common.domain.entity.ResAccessoryFileInfoLabel;
import com.fi.common.mapper.ResAccessoryFileInfoLabelMapper;
import com.fi.common.mapper.ResAccessoryFileInfoMapper;
import com.fi.common.service.ResAccessoryFileInfoLabelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author songqiang
 * @desc 公告标签关联服务类
 * @created on 2023 年 09 月 05
 * @Date 17:10
 * @since 1.0
 */

@Service
@RequiredArgsConstructor
public class ResAccessoryFileInfoLabelServiceImpl extends ServiceImpl<ResAccessoryFileInfoLabelMapper, ResAccessoryFileInfoLabel> implements ResAccessoryFileInfoLabelService {

    private final ResAccessoryFileInfoMapper resAccessoryFileInfoMapper;

    @Override
    public ResAccessoryFileInfo assembleFileInfoLabel(Long accessId) {
        ResAccessoryFileInfo info = resAccessoryFileInfoMapper.selectById(accessId);
        if (Objects.isNull(info)) {
            return null;
        }
        assembleFileInfoLabel(Collections.singletonList(info));
        return info;
    }


    // -----

    @Override
    public void assembleFileInfoLabel(List<ResAccessoryFileInfo> fileInfoList){
        if (CollUtil.isEmpty(fileInfoList)) {
            return;
        }
        List<Long> ids = fileInfoList.stream().map(ResAccessoryFileInfo::getId).collect(Collectors.toList());
        List<ResAccessoryFileInfoLabel> fileInfoLabels = lambdaQuery().in(ResAccessoryFileInfoLabel::getId, ids).list();
        assembleFileInfoLabel(fileInfoList,fileInfoLabels);
    }


    // -----

    @Override
    public void assembleFileInfoLabel(List<ResAccessoryFileInfo> fileInfoList, Collection<ResAccessoryFileInfoLabel> fileInfoLabels) {
        Map<Long, ResAccessoryFileInfoLabel> fileInfoLabelMap = fileInfoLabels.stream().collect(Collectors.toMap(ResAccessoryFileInfoLabel::getId, item -> item));
        fileInfoList.forEach(item -> {
            ResAccessoryFileInfoLabel infoLabel = fileInfoLabelMap.get(item.getId());
            if (Objects.nonNull(infoLabel)) {
                BeanUtil.copyProperties(infoLabel, item, CopyOptions.create().ignoreNullValue().setIgnoreProperties("updateTime", "createTime", "updateUser"));
            }
        });
    }


}
